//
//  HZEmoticonPageView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/18.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
// cell可重用标识符
private let HZEmoticonPageViewCellId = "HZEmoticonPageViewCellId"

class HZEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置视图
    private func setupUI(){
        
        backgroundColor = randomColor()
        
        registerClass(HZEmoticonPageViewCell.self, forCellWithReuseIdentifier: HZEmoticonPageViewCellId)
        // 代理
        dataSource = self
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = self.frame.size
        // 设置间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        // 滚动方向
        layout.scrollDirection = .Horizontal
        
    }
    
}



extension HZEmoticonPageView: UICollectionViewDataSource {
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return HZEmticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return HZEmticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HZEmoticonPageViewCellId, forIndexPath: indexPath) as! HZEmoticonPageViewCell
        cell.indexPath = indexPath
        return cell
    }
    
}